package com.wushixin.springdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wushixin.springdemo.dto.UserDto;
import com.wushixin.springdemo.pojo.ModifyPwd;
import com.wushixin.springdemo.pojo.User;
import com.wushixin.springdemo.pojo.utils.Result;
import com.wushixin.springdemo.service.UserService;
import lombok.extern.slf4j.Slf4j;
import lombok.val;
import net.sf.jsqlparser.Model;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping(value = "/user")
public class UserController {

    @Autowired
    private UserService userService;
    @PostMapping("/login")
    public Result login(HttpSession session, @RequestBody User user) {
        Boolean flag = userService.check(user);
        LambdaQueryWrapper<User> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(user.getName()!=null, User::getName,user.getName());
        lambdaQueryWrapper.eq(user.getPwd()!=null, User::getPwd,user.getPwd());
        User one = userService.getOne(lambdaQueryWrapper);
        System.out.println("登录验证====》" + flag);

        session.setAttribute("user",one.getId());
        Object user1 = session.getAttribute("user");
        System.out.println(user1);
        return new Result(one,flag);
    }
    @GetMapping("logout")
    public Result logout(HttpSession session) {
        log.info("退出登录");
        session.removeAttribute("user");
        return new Result(true,"退出成功");
    }
    @GetMapping
    public Result GetAll() {
        return  new Result(userService.list(),userService.list()!=null?true:false);
    }
    @GetMapping("{id}")
    public Result GetById(@PathVariable Integer id) {
        return new Result(userService.getById(id),true);
    }

    @PostMapping
    public Result saveStudent(@RequestBody User user) {
        return new Result(null,userService.save(user));
    }

    @DeleteMapping("{id}")
    public Result deleteStudent(@PathVariable Integer id) {
        boolean flag = userService.removeById(id);
        return new Result(null,flag);
    }

    @PutMapping
    public Result updateStudent(@RequestBody User user) {
        Boolean flag = userService.updateById(user);

        return new Result(flag,flag?"添加成功":"添加失败");
    }

    @GetMapping("{current}/{pageSize}")
    public Result pageStudent(@PathVariable Integer current,@PathVariable Integer pageSize, User user) {

        Page<User> page = new Page<User>();
        if (user.getName() != null || user.getPwd() != null || user.getState() != null) {
            System.out.println(" ============= 参数为不空 ============= ");
            page= userService.getPage(current, pageSize,user);
            if(current > page.getPages()) {
                page = userService.getPage((int) page.getPages(),pageSize,user);
            }

        }else {
            System.out.println(" ============= 参数为空 ============= ");
            page = userService.getPage(current,pageSize);
            if(current > page.getPages()) {
                page = userService.getPage((int) page.getPages(),pageSize);
            }
        }

        return new Result(page,page != null ? true:false);
    }

    @PostMapping("/modify")
    public Result modifyPwd(HttpSession session,@RequestBody UserDto userDto) {
        Long userId = (Long) session.getAttribute("user");
        User user = new User();
        BeanUtils.copyProperties(userDto,user,"confirmpwd");
//        查找该用户
        val modifyPwdLambdaQueryWrapper = new LambdaQueryWrapper<User>();
        modifyPwdLambdaQueryWrapper.eq(user.getName()!=null,User::getName,user.getName());
        User userInfo = userService.getOne(modifyPwdLambdaQueryWrapper);
        if (userInfo == null) {
            return new Result(false,"没有该用户");
        }else if (!userInfo.getId().equals(userId)) {
            return new Result(false,"不能修改其他用户");
        } else {
//        比对两次密码是否一样
            if (!userDto.getPwd().equals(userDto.getConfirmpwd())) {
                return new Result(false,"两次输入的密码不一样");
            }
            userInfo.setPwd(userDto.getPwd());
            //        修改密码
            userService.updateById(userInfo);
            return new Result(true,"修改成功");
        }

    }
}
